package com.biz.primus.ms.coupon.trans;

import com.biz.primus.model.coupon.vo.coupon.CouponVo;
import com.biz.primus.ms.coupon.dao.ro.CouponRo;
import com.biz.primus.ms.coupon.dao.ro.CouponTypeRo;
import com.google.common.base.Function;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

/**
 * 转换器 CouponRo --> CouponVo
 *
 * @author david-liu
 */
public class CouponRo2CouponVo implements Function<CouponRo, CouponVo> {

    private CouponTypeRo typeRo;

    public CouponRo2CouponVo(CouponTypeRo typeRo) {
        this.typeRo = typeRo;
    }

    @Override
    public CouponVo apply(CouponRo ro) {
        if (Objects.isNull(typeRo)) {
            return null;
        }
        CouponVo vo = new CouponVo();
        vo.setId(ro.getId());
        vo.setName(typeRo.getCouponTypeName());
        vo.setFaceValue(typeRo.getFaceValue());
        vo.setCouponAmountLimit(typeRo.getCouponLimitAmount());
        vo.setDescription(typeRo.getCouponTypeDesc());
        vo.setCouponLimitType(typeRo.getCouponLimitType().getDesc());
        vo.setCouponIcon(typeRo.getIcon());
        vo.setCouponScopeType(typeRo.getCouponScopeType().getDesc());
        LocalDateTime usableStartTime = typeRo.getUsableStartTime();
        LocalDateTime usableEndTime = typeRo.getUsableEndTime();
        Integer usableCountDownDays = typeRo.getUsableCountDownDays();
        if (Objects.nonNull(usableCountDownDays)) {
            LocalDateTime countDownDateTime = usableStartTime.plusDays(usableCountDownDays);
            usableEndTime = usableEndTime.isAfter(countDownDateTime) ? countDownDateTime : usableEndTime;
        }
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy.M.d");
        vo.setCouponAvailableDuration(String.format("%s - %s", dateTimeFormatter.format(usableStartTime), dateTimeFormatter.format(usableEndTime)));
        return vo;
    }
}
